package com.lifp.thread.synchronize;

/**
 * @Description 死锁演示
 * @Author lfp
 * @Date 2022/7/26
 * @Version 1.0
 */
public class DeadLock {

    //创建两个对象
    static Object a = new Object();
    static Object b = new Object();


    public static void main(String[] args) {
        //创建两个线程，争夺锁

        new Thread(()->{
            synchronized (a){
                System.out.println(Thread.currentThread().getName() + "持有锁a，试图获取锁b");

                synchronized (b){
                    System.out.println(Thread.currentThread().getName() + "获得锁b");
                }
            }
        },"A").start();

        new Thread(()->{
            synchronized (b){
                System.out.println(Thread.currentThread().getName() + "持有锁b，试图获取锁a");

                synchronized (a){
                    System.out.println(Thread.currentThread().getName() + "获得锁a");
                }
            }
        },"B").start();
    }
}
